//
//  ForeignView.m
//  ITrip
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ForeignView.h"
#import "HeaderView.h"
@implementation ForeignView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMyView];
    }
    return self;
}



- (void)createMyView
{
    NSString *path = [URLPath strategyURL];
    self.arrayData = [NSMutableArray array];
    //下载
    //创建一个AFN管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //告诉AFN不要解析
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"1"};
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.arrayData = [StrategyParser getParser:operation.responseData];
        [self initTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    

}

- (void)initTableView
{
    //创建一个布局类
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置大小（一个方块的大小）
    layout.itemSize = CGSizeMake(self.frame.size.width / 2 - 20, 200);
    //设置最小行距
    layout.minimumLineSpacing = 10;
    //设置最小列距离
    layout.minimumInteritemSpacing = 10;
    //上下左右边距
//    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);

    self.myCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.frame.size.width, self.frame.size.height - 64) collectionViewLayout:layout];
    
    self.myCollect.delegate = self;
    self.myCollect.dataSource = self;
    //注册cell
    [self.myCollect registerClass:[StrategyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.myCollect registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HearderView"];
    self.myCollect.showsVerticalScrollIndicator = NO;
    
    //加背景颜色
    self.myCollect.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    [self addSubview:self.myCollect];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HearderView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headView.label.text = @"亚    洲";
        } else if (indexPath.section == 1) {
            headView.label.text = @"欧    洲";
        } else {
            headView.label.text = @"其    他";
        }
        
        reusableView = headView;
    }
    return reusableView;
}

//小方块的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        NSArray *array = [self.arrayData objectAtIndex:0];
        return array.count;
    } else if (section == 1) {
        NSArray *array = [self.arrayData objectAtIndex:1];
        return array.count;
    } else {
        NSArray *array = [self.arrayData objectAtIndex:2];
        return array.count;
    }
    
    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.frame.size.width, 30);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
    StrategyModel *model = [array objectAtIndex:indexPath.row];
    [cell setStrategyModel:model];
    return cell;
}

//seg的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate sendValue:indexPath.section :indexPath.row];
//    NSLog(@"%ld", indexPath.row);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
